import numpy as np
from matplotlib import pyplot as plt
from openpyxl import load_workbook

random = {}
weighted = {}

wb = load_workbook('./数据生成算法实验.xlsx')
sheet = wb['运行时间']

for r in range(2, 26):
    algorithm = sheet.cell(row=r, column=1).value
    name = sheet.cell(row=r, column=2).value
    time = sheet.cell(row=r, column=4).value
    iter = sheet.cell(row=r, column=5).value
    shape = sheet.cell(row=r, column=6).value
    s = 1
    for t in eval(shape):
        s *= t
    if algorithm == 'random':
        random[name] = float(time)
    else:
        weighted[name] = float(time)

x = ['bias_add', 'avg_pool', 'softmax', 'conv2d', 'batch_norm', 'max_pool',
     'relu', 'reduce_mean', 'reduce_max', 'sigmoid', 'tanh', 'matmul']

# plt.title('Consumed Time of Weighted and Random Sampling')

y_random = [random[i] for i in x]
y_random.append(101.225307)
y_random.append(800)
y_random.append(4.185871601)
y_random.append(800)
y_weighted = [weighted[i] for i in x]
y_weighted.append(11.69016075)
y_weighted.append(14.42016554)
y_weighted.append(1.651990414)
y_weighted.append(1.434106827)

y_random = y_random[0: 2] + y_random[3: 9] + y_random[11: 13] + y_random[14:]
y_weighted = y_weighted[0: 2] + y_weighted[3: 9] + y_weighted[11: 13] + y_weighted[14:]


x = ['op1', 'op2', 'op3', 'op4', 'op5', 'op6', 'op7', 'op8', 'op9', 'op10',
     'op11', 'op12']

line1, = plt.plot(x, y_weighted)
line2, = plt.plot(x, y_random)

# ax = plt.gca()
# ax.set_ylim(0, 27)
# ax.set_yticklabels(['0','3','6','9','12', '15', '18', '21', '', '1000+'], fontsize=7)

plt.xlabel("Op(s)",fontsize=9, labelpad=-1)
plt.ylabel("Time(s)",fontsize=9, labelpad=10)
# new_ticks = np.linspace(0, 27, 10)
# plt.yticks(new_ticks, ['0','3','6','9','12', '15', '18', '21', '...', '1633'], fontsize=7)
plt.xticks(fontsize=8, x=x, rotation=330)
plt.yticks(ticks=np.linspace(0, 800, 9), labels=['0', '100', '200', '300', '400', '500', '600', '700', '∞'], fontsize=9)

plt.legend(handles=[line1, line2], labels=['$D_{pm}$', '$D_{r}$'],
           loc='upper center', fontsize=10)

plt.savefig('./plott/time1.eps', format='eps', dpi=1000)
plt.show()
